/**
 * @description  流动性风险
 * @author shenhong
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { TableC } from '../../../comps';

class TableOne extends PureComponent {
  constructor() {
    super();
    this.state = {
      data: { columns: undefined, dataSource: undefined },
    };
  }

  componentDidMount() {
    const { dispatch, formData, portfolioCode, args } = this.props;

    const query = {
      portfolioCode: portfolioCode,
      dataDate: formData.dataDate,
      stockRate: args.stockRate,
      stockDays: args.stockDays,
      bondRate: args.bondRate,
      bondDays: args.bondDays,
      FundRate: args.FundRate,
      FundDays: args.FundDays,
    };

    if (formData.benchMark) {
      query.benchMark = formData.benchMark;
    }

    query.currentPage = 1;
    query.numInPage = 10000;
    dispatch({
      type: 'liquidityRiskM/getTableDataOne',
      payload: query,
    });
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const { formData, portfolioCode, args } = nextProps;
    if (
      portfolioCode !== this.props.portfolioCode ||
      formData.dataDate !== this.props.formData.dataDate ||
      formData.benchMark !== this.props.formData.benchMark ||
      JSON.stringify(args) !== JSON.stringify(this.props.args)
    ) {
      const { dispatch } = this.props;
      const query = {
        portfolioCode: portfolioCode,
        dataDate: formData.dataDate,
        stockRate: args.stockRate,
        stockDays: args.stockDays,
        bondRate: args.bondRate,
        bondDays: args.bondDays,
        FundRate: args.FundRate,
        FundDays: args.FundDays,
      };
      if (formData.benchMark) {
        query.benchMark = formData.benchMark;
      }
      query.currentPage = 1;
      query.numInPage = 10000;
      dispatch({
        type: 'liquidityRiskM/getTableDataOne',
        payload: query,
      });
    }
  }

  changeFlowType = (text) => {
    let tet = null;
    if (text !== undefined && text !== '' && text !== null) {
      tet = `${text}%`;
    } else {
      tet = '--';
    }
    return tet;
  };

  changeColumns = (columns) => {
    let newColumns = columns;
    if (newColumns && newColumns.length) {
      newColumns.map((item) => {
        if (item.key == 'portHoldRatio' || item.key == 'benchHoldRatio') {
          item.align = 'right';
          item.render = (text, record) => {
            return this.changeFlowType(text);
          };
        }
      });
    }
    return newColumns;
  };

  ShowTable = () => {
    const {
      liquidityRiskM: { tableDataOne },
      loading,
    } = this.props;
    const columns = this.changeColumns(tableDataOne.columns);
    const dataSource = tableDataOne.dataSource;
    return (
      <div>
        <TableC columns={columns} dataSource={dataSource} hidePage={true} loading={loading} rowKey='conflictFreeRealizationDays' />
      </div>
    );
  };

  render() {
    return <div>{this.ShowTable()}</div>;
  }
}
export default connect(({ liquidityRiskM, loading }) => ({ liquidityRiskM, loading: loading.effects['liquidityRiskM/getTableDataOne'] }))(TableOne);
